
import Vue from 'vue';
class EventBusClass extends Vue{

    constructor() {
        super()
        this.listeners = []
    }

    on(event,callback,context){
        this.$on(event,callback)
        this.listeners.push({event,callback,context})
    }

    off(event, callback, context){
        this.$off(event, callback)
        this.listeners = this.listeners.filter(listener =>
            listener.event !== event || listener.callback !== callback || listener.context !== context
        )
    }

    clear(context) {
        this.listeners = this.listeners.filter(listener => {
          if (listener.context === context) {
            this.$off(listener.event, listener.callback)
            return false;
          }
          return true;
        })
      }

}

const EventBus = new EventBusClass()
export default EventBus
